<?php

namespace App\Http\Controllers\Admin;

use App\Library\Other\ProductNewsManagers;
use App\Models\ProductNews;
use Illuminate\Http\Request;

class ProductNewsController extends BaseController
{
    use ProductNewsManagers;

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $items = ProductNews::with('user')->orderByDesc('id')->paginate(20);

        return $this->view('admin.productnews.sales', [
            'items'=>$items,
            'pagination'=>$items->render()
        ]);
    }

    public function deleteAll(Request $request)
    {
        $items = $request->input('items', []);
        ProductNews::whereIn('id', $items)->get()->map(function ($news){
            $news->delete();
        });

        return $this->messager()->setMessage(trans('sysmessage.info delete success'))->render();
    }

    public function setImage(Request $request)
    {
        $id = $request->input('id', 0);
        $image = $request->input('image', '');
        ProductNews::where('id', $id)->update(['image'=>$image]);
        return ajaxReturn();
    }
}
